package com.ruoyi.web.core.config;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.ruoyi.framework.config.properties.MongoProperties;
import org.flowable.engine.*;
import org.flowable.mongodb.cfg.MongoDbProcessEngineConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @description: 流程引擎配置文件
 * @author: gankench@gmail.com
 * @create: 2021-04-20 21:35
 */
@Configuration
public class ProcessEngineConfig {

    private Logger logger = LoggerFactory.getLogger(ProcessEngineConfig.class);

    @Autowired
    private MongoProperties prop;

    private static final String template = "mongodb://{}:{}@{}/{}";

    /**
     * 初始化流程引擎
     *
     * @return
     */
    @Primary
    @Bean(name = "processEngine")
    public ProcessEngine initProcessEngine() {
        logger.info("=============================ProcessEngineBegin=============================");
        String address = ArrayUtil.join(prop.getAddress().toArray(new String[prop.getAddress().size()]), ",");
        String mongoClientURI = StrUtil.format(template, prop.getUserName(), prop.getPassword(), address, prop.getAuthDB());
        MongoClientURI connectionString = new MongoClientURI(mongoClientURI);
        MongoClient mongoClient = new MongoClient(connectionString);
        // 流程引擎配置
        ProcessEngineConfiguration cfg = null;
        try {
            cfg = new MongoDbProcessEngineConfiguration()
                    .setMongoClient(mongoClient)
                    .setDatabaseName(prop.getDatabase())
                    .setDisableIdmEngine(true)
                    // 初始化基础表，不需要的可以改为 DB_SCHEMA_UPDATE_FALSE
                    .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
                    // 默认邮箱配置
                    // 发邮件的主机地址，先用 QQ 邮箱
                    .setMailServerHost("smtp.qq.com")
                    // POP3/SMTP服务的授权码
                    .setMailServerPassword("hergynqfusnsbbdf")
                    // 默认发件人
                    .setMailServerDefaultFrom("406676933@qq.com")
                    // 设置发件人用户名
                    .setMailServerUsername("管理员");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 初始化流程引擎对象
        ProcessEngine processEngine = cfg.buildProcessEngine();
        logger.info("=============================ProcessEngineEnd=============================");
        return processEngine;
    }

    /**
     * @param processEngine
     * @description: //八大接口
     * @return: org.flowable.engine.RepositoryService
     * @author: gankench@gmail.com
     * @time: 4/21/21 5:20 PM
     */
    @Bean
    public RepositoryService repositoryService(ProcessEngine processEngine) {
        return processEngine.getRepositoryService();
    }

    @Bean
    public RuntimeService runtimeService(ProcessEngine processEngine) {
        return processEngine.getRuntimeService();
    }

    @Bean
    public TaskService taskService(ProcessEngine processEngine) {
        return processEngine.getTaskService();
    }

    @Bean
    public HistoryService historyService(ProcessEngine processEngine) {
        return processEngine.getHistoryService();
    }

    @Bean
    public FormService formService(ProcessEngine processEngine) {
        return processEngine.getFormService();
    }

    @Bean
    public IdentityService identityService(ProcessEngine processEngine) {
        return processEngine.getIdentityService();
    }

    @Bean
    public ManagementService managementService(ProcessEngine processEngine) {
        return processEngine.getManagementService();
    }

    @Bean
    public DynamicBpmnService dynamicBpmnService(ProcessEngine processEngine) {
        return processEngine.getDynamicBpmnService();
    }
}
